package com.maxmlr.music.admin.controller;

import com.maxmlr.music.admin.entity.Menu;
import com.maxmlr.music.admin.param.TreeParam;
import com.maxmlr.music.admin.service.MenuService;
import com.maxmlr.music.common.base.BaseController;
import com.maxmlr.music.common.bean.ResultBean;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;

/**
 * @author max
 * @version 1.0
 * @ClassName MenuController
 * @Description 菜单管理 控制器
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-14
 */
@Slf4j
@RestController
@Api(value = "/admin/menu", tags = "Menu 控制器")
@RequestMapping("/admin/menu")
public class MenuController extends BaseController<MenuService, Menu> {

    @Autowired
    public MenuService service;

    /**
     * 保存
     *
     * @param menu
     * @return ResultBean<Boolean>
     */
    @PostMapping
    @RequiresPermissions("admin:menu:add" )
    public ResultBean<Boolean> save(Menu menu) {
        return ResultBean.ok(service.save(menu));
    }

    /**
     * 修改
     *
     * @param menu
     * @return ResultBean<Boolean>
     */
    @PutMapping
    @RequiresPermissions("admin:menu:edit" )
    public ResultBean<Boolean> update(Menu menu) {
        return ResultBean.ok(service.updateById(menu));
    }

    /**
     * 删除
     *
     * @param id
     * @return ResultBean<Boolean>
     */
    @DeleteMapping("/remove/{id}" )
    @RequiresPermissions("admin:menu:remove" )
    public ResultBean<Boolean> remove(@PathVariable("id" ) Serializable id) {
        return ResultBean.ok(service.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return ResultBean<Boolean>
     */
    @DeleteMapping("/batch-remove")
    @RequiresPermissions("admin:menu:batch-remove")
    public ResultBean<Boolean> batchRemove(@RequestParam("ids[]") Serializable[] ids){
        return ResultBean.ok(service.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping("/tree")
    public ResultBean<TreeParam<Menu>> tree() {
        return ResultBean.ok(service.getTreeParam());
    }

    @GetMapping("/tree/{roleId}")
    @ResponseBody
    public ResultBean<TreeParam<Menu>> tree(@PathVariable("roleId") Long roleId) {
        return ResultBean.ok(service.getTreeParam(roleId));
    }

}
